package com.lvtn.adsonandroid;

import java.util.ArrayList;

import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FollowerAdsActivity extends Activity {
	private static String CancelFollowerAdsURL = "http://10.0.2.2/android/CancelFollower.php";
	private static String UpdateStatusAdsURL = "http://10.0.2.2/android/UpdateStatus.php";
	private ProgressDialog pDialog;
	JSONParser jParser = new JSONParser();
    private String title;
    private String news_id;
    private String detail;
    private String date;
    private String user;
    private String link;
    private String email;
    private String email_com;
    private String unique_id;
    private TextView tieude;
    private TextView chitiet;
    private TextView ngaythang;
    private TextView ten;
    private ImageLoader imageloader;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_follower_ads);
		Intent t = getIntent();
		title = t.getStringExtra("title");
		detail = t.getStringExtra("detail");
		news_id = t.getStringExtra("news_id");
		date = t.getStringExtra("date");
		user = t.getStringExtra("user_name");
		link = t.getStringExtra("link");
		email = t.getStringExtra("user_email");
		email_com = t.getStringExtra("email_comuser");
		Log.d("linkamge",link);
		//title = t.getStringExtra("title");
		tieude = (TextView)findViewById(R.id.title);
		ngaythang = (TextView)findViewById(R.id.date);
		chitiet = (TextView)findViewById(R.id.detail);
		ten = (TextView)findViewById(R.id.user_name);
		tieude.setText(title);
		ngaythang.setText(date);
		chitiet.setText(detail);
		ten.setText(user);
		Log.d("tenuser",user);
		//new DownloadImageTask((ImageView) findViewById(R.id.image)).execute(link);
		imageloader = new ImageLoader(getApplicationContext());
		imageloader.DisplayImage(link, (ImageView) findViewById(R.id.image));
		findViewById(R.id.cancel_follower).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
							new Cancel_Follower().execute();
					}
				});
		findViewById(R.id.see_user).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent intent = new Intent(getApplicationContext(),PublisherInfoActivity.class);
					      intent.putExtra("email", email);
					      startActivity(intent);
					}
				});
		findViewById(R.id.see_comment).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent intent = new Intent(getApplicationContext(),GetCommentActivity.class);
					    intent.putExtra("email_user", email);
					    intent.putExtra("email_comuser", email_com);
					    startActivity(intent);
					}
				});
		findViewById(R.id.see_images).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent intent = new Intent(getApplicationContext(),GalleryViewActivity.class);
					    intent.putExtra("news_id", news_id);
					    startActivity(intent);
					}
				});
		findViewById(R.id.update_status).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						final EditText input = new EditText(FollowerAdsActivity.this);
						new AlertDialog.Builder(FollowerAdsActivity.this)
					    .setTitle("Update Status")
					    .setMessage("Bạn hãy nhập mã tin đăng")
					    .setView(input)
					    .setPositiveButton("Xong", new DialogInterface.OnClickListener() {
					        public void onClick(DialogInterface dialog, int whichButton) {
					             unique_id = input.getText().toString(); 
					             new UpdateStatus().execute();
					        }
					    }).setNegativeButton("Bỏ qua", new DialogInterface.OnClickListener() {
					        public void onClick(DialogInterface dialog, int whichButton) {
					            // Do nothing.
					        }
					    }).show();
						
					}
				});
	}
class Cancel_Follower extends AsyncTask<String, String, Boolean> {
		
		/**
		* Before starting background thread Show Progress Dialog
		* */
			@Override
			protected void onPreExecute() {
				Log.d("asdasdasda","adasdada");
				super.onPreExecute();
				pDialog = new ProgressDialog(FollowerAdsActivity.this);
				pDialog.setMessage("Sending...");
				pDialog.setIndeterminate(false);
				pDialog.setCancelable(false);
				pDialog.show();
			}
		/**
		* getting All record data from url
		* */
			protected Boolean doInBackground(String... args) {
		// Building Parameters
				Log.d("asdasdasda","adasdada");
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("tag", "CancelFollowerAds"));
				params.add(new BasicNameValuePair("news_id", news_id));
			//	params.add(new BasicNameValuePair("email_user", email_com));
			//	params.add(new BasicNameValuePair("email_comuser", email_comuser));
			//	params.add(new BasicNameValuePair("comment", comment));
		// getting JSON string from URL
				JSONObject json = jParser.makeHttpRequest(CancelFollowerAdsURL, "POST", params);
		// Check your log cat for JSON reponse
				Log.d("CancelFollowerAds: ", json.toString());
				try {
		// Checking for SUCCESS TAG
					int success = json.getInt("success");
					if (success == 1) {
				         return true;
						}
					 else {
				
						return false;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			return null;
			}
		/**
		* After completing background task Dismiss the progress 
		dialog
		* **/
			protected void onPostExecute(Boolean success) {
		
				if (success) {
					pDialog.dismiss();
					Toast toast = Toast.makeText(getApplicationContext(), "Đã bỏ theo dõi", Toast.LENGTH_SHORT);
					toast.show();
					//finish();
				}
				
				else{
			// updating UI from Background Thread
					pDialog.dismiss();
					Toast toast = Toast.makeText(getApplicationContext(), "Bỏ theo dõi không thành công", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		}
class UpdateStatus extends AsyncTask<String, String, Boolean> {
	
	/**
	* Before starting background thread Show Progress Dialog
	* */
		@Override
		protected void onPreExecute() {
			Log.d("asdasdasda","adasdada");
			super.onPreExecute();
			pDialog = new ProgressDialog(FollowerAdsActivity.this);
			pDialog.setMessage("Sending...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}
	/**
	* getting All record data from url
	* */
		protected Boolean doInBackground(String... args) {
	// Building Parameters
			Log.d("asdasdasda","adasdada");
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("tag", "UpdateStatusAds"));
			params.add(new BasicNameValuePair("news_id", news_id));
			params.add(new BasicNameValuePair("unique_id", unique_id));
		//	params.add(new BasicNameValuePair("email_comuser", email_comuser));
		//	params.add(new BasicNameValuePair("comment", comment));
	// getting JSON string from URL
			JSONObject json = jParser.makeHttpRequest(UpdateStatusAdsURL, "POST", params);
	// Check your log cat for JSON reponse
			Log.d("UpdateStatusAds: ", json.toString());
			try {
	// Checking for SUCCESS TAG
				int success = json.getInt("success");
				if (success == 1) {
			         return true;
					}
				 else {
			
					return false;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		return null;
		}
	/**
	* After completing background task Dismiss the progress 
	dialog
	* **/
		protected void onPostExecute(Boolean success) {
	
			if (success) {
				pDialog.dismiss();
				Toast toast = Toast.makeText(getApplicationContext(), "Đã cập nhật thành công", Toast.LENGTH_SHORT);
				toast.show();
				//finish();
			}
			
			else{
		// updating UI from Background Thread
				pDialog.dismiss();
				Toast toast = Toast.makeText(getApplicationContext(), "Cập nhật không thành công", Toast.LENGTH_SHORT);
				toast.show(); 
			}
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.follower_ads, menu);
		return true;
	}

}
